#include <Gal.h>
#include <stdio.h>
#include <stdlib.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <nouveau_device.h>
#include <nouveau_bo.h>
#include "Private.h"
#include <fcntl.h>

/// <summary>
/// TODO
/// </summary>
DisplayPtr NVCreateDisplay(void)
{
	DisplayPtr dsp;
	if (!(dsp = (DisplayPtr)malloc(sizeof(Display))))
		return NULL;
	return dsp;
}

/// <summary>
/// Display
/// </summary>
int NVInitDisplays(DriverPtr drv, int handle)
{
	int i = 0;
	drmModeRes *resources;
	if (!(resources = drmModeGetResources(handle)))
		return -1;

	printf("CRTC:\n");
	printf("\tCount: %d\n", resources->count_crtcs);
   	for (i = 0; i < resources->count_crtcs; i++)
	{
		drmModeCrtcPtr crtc;
		crtc = drmModeGetCrtc(handle, resources->crtcs[i]);
		if (crtc == NULL)
			continue;

		printf("CRTC:\n");	
		printf("Left: %d\n", crtc->x);
		printf("Top: %d\n", crtc->y);
		printf("Width: %d\n", crtc->width);
		printf("Height: %d\n", crtc->height);
		printf("FB: %d\n", crtc->buffer_id);
		
		if (crtc->buffer_id > 0) {
		drmModeFBPtr fbo;
		fbo = drmModeGetFB(handle, crtc->buffer_id);
		if (fbo != NULL) {
			printf("\tFBO:\n");	
			printf("\tDepht: %d\n", fbo->depth);
			printf("\tWidth: %d\n", fbo->width);
			printf("\tHeight: %d\n", fbo->height);
			printf("\tBpp: %d\n", fbo->bpp);
			printf("\tPitch: %d\n", fbo->pitch);
			drmModeFreeFB(fbo);
		}
		}

		drmModeFreeCrtc(crtc);
	}

	drmModeFreeResources(resources);

	return 0;
}

/// <summary>
/// TODO
/// </summary>
int NVSetCursorPosition(DisplayPtr dsp, int x, int y)
{
	if (dsp == NULL)
		return -1;
	return 0;
}

/// <summary>
/// TODO
/// </summary>
int DrvInitDisplayFramebuffer()
{
	return 0;
}

